package br.ufpe.cin.analysis.plugin.views;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Vector;

import javax.xml.bind.JAXBException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.internal.ole.win32.COMObject;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;

import br.ufpe.cin.analysis.plugin.entities.Activity;
import br.ufpe.cin.analysis.plugin.entities.Lane;
import br.ufpe.cin.analysis.plugin.entities.Metric;
import br.ufpe.cin.analysis.plugin.entities.NewProcess;
import br.ufpe.cin.analysis.plugin.io.XPDLReader;
import br.ufpe.cin.analysis.plugin.models.XMLMannager;

public class SimulationConfigTab extends Composite {
	
	private TabItem tabItem;
	private Combo comboLane, comboActivity;
	private Button bLoadXpdl, bStartSimulation;
	private Label lLane, lComboLane, lActivity, lComboActivity, lResources, lDuration, lTitle;
	private Text tResources, tDuration;
	
	private ComboLaneListener comboLaneListener;
	private LoadXPDLListener loadXPDLListener;
	private ComboActivityListener comboActivityListener;
	private StartSimulationListener startSimulationListener;
	
	private Vector<Lane> lanes;

	public SimulationConfigTab(TabFolder parent, int style) {
		super(parent, style);
		
		this.comboLaneListener = new ComboLaneListener();
		this.loadXPDLListener = new LoadXPDLListener();
		this.comboActivityListener = new ComboActivityListener();
		this.startSimulationListener = new StartSimulationListener();
		
		this.lanes = null;
		
		this.tabItem = new TabItem(parent, SWT.NONE);
		this.tabItem.setText("Parameters");
		this.tabItem.setControl(this);
		
		GridLayout gridL = new GridLayout(2, false);
		gridL.verticalSpacing = 10;
		this.setLayout( gridL );
		
		GridData gd = null;
		
		gd = new GridData(GridData.FILL_HORIZONTAL);
		
		gd.horizontalSpan = 2;
		
		this.lTitle = new Label(this, SWT.NONE);
		
		this.lTitle.setText("Parameters");
		
		
		
		//Changing font

		this.lTitle.setFont(this.getNewFont(16));
		
		this.lTitle.setLayoutData(gd);
		
		
		//Default Layouts
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.verticalSpan = 4;
		
		
		GridData gd2 = new GridData(GridData.FILL_HORIZONTAL);
		
		gd2.horizontalAlignment = GridData.END;
		gd2.horizontalSpan = 2;
		
		
		//Creating ComboLane
		this.comboLane = new Combo(this, SWT.READ_ONLY | SWT.DROP_DOWN);
		this.comboLane.setLayoutData( gd );
		
		this.comboLane.addSelectionListener(comboLaneListener);
		
		
		//Creating Load XPDL button
		this.bLoadXpdl = new Button( this, SWT.PUSH);
		this.bLoadXpdl.setText("Load XPDL");
		this.bLoadXpdl.setLayoutData(gd);
		this.bLoadXpdl.addListener(SWT.Selection, loadXPDLListener);
		
		
		//Creating lResources
		this.lResources = new Label(this, SWT.NONE);
		this.lResources.setText("Lane Resources ");
		this.lResources.setLayoutData(gd);
		
		//Creating tResources
		this.tResources = new Text(this, SWT.BORDER | SWT.WRAP);
		this.tResources.setLayoutData(gd);
		
		//Creating lAcitivty
		this.lActivity = new Label( this, SWT.NONE);
		this.lActivity.setLayoutData(gd);
		this.lActivity.setText("Choose an Activity ");
		
		
		//Creating ComboActivity
		
		this.comboActivity = new Combo( this, SWT.READ_ONLY|SWT.DROP_DOWN);
		this.comboActivity.setLayoutData(gd);
		this.comboActivity.addSelectionListener(comboActivityListener);
		
		//Creating lDuration	
		
		this.lDuration = new Label( this, SWT.NONE);
		this.lDuration.setLayoutData(gd);
		this.lDuration.setText("Activity Duration ");
		
		
		//Creating tDuration
		
		this.tDuration = new Text(this,SWT.BORDER | SWT.WRAP);
		this.tDuration.setLayoutData(gd);
		
		//Creating bStartSimulation
		
		this.bStartSimulation = new Button(this, SWT.PUSH);
		this.bStartSimulation.setLayoutData(gd2);
		this.bStartSimulation.setText("Start Simulation");
		this.bStartSimulation.addListener(SWT.Selection,startSimulationListener);

		
		
		
		
		
		
		
		
		
		
		
	}
	
	
	private Font getNewFont(int size)
	{
		Text text = new Text(this, SWT.MULTI | SWT.BORDER);
		Font initialFont = text.getFont();
		FontData[] fontData = initialFont.getFontData();
		
		for (int i = 0; i < fontData.length; i++) {
			fontData[i].setHeight(size);
			
		}
		Font font = new Font(this.getDisplay(), fontData);
		
		text.dispose();
		
		
		return font;
	}
	
	private class LoadXPDLListener implements Listener {

		public void handleEvent(Event arg0) {
			if ( arg0.type == SWT.Selection)
			{
				FileDialog fd = new FileDialog(getShell(), SWT.OPEN);
				fd.setText("Choose File Dialog");
				String path = fd.open();
				
				
				
				XPDLReader reader = null;
				
				
				try {
					reader = new XPDLReader(new File(path));

				} catch (FileNotFoundException e) {
					e.printStackTrace();
				}
				
				lanes = reader.getLanes();
				
			
				comboLane.removeAll();
				String str[] = new String[lanes.size()];
				for( int i = 0; i < str.length; i++) {
					str[i] = lanes.elementAt(i).getName();
					comboLane.setData(str[i], lanes.elementAt(i));
					
					for( Activity ac : lanes.elementAt(i).getActivities() )
					{
						ac.getMetrics().add(new Metric("Duration", 0.0));
					}
					
				}
				
			
				comboLane.setItems(str);
				
				comboLane.select(0);
				comboLaneListener.widgetSelected(null);
				
			}
			
		}
		
	}
	
	private class ComboLaneListener implements SelectionListener
	{
		private Lane previousSelection;
		
		public ComboLaneListener()
		{
			this.previousSelection = null;
		}
		
		public void widgetDefaultSelected(SelectionEvent arg0) {
			
			
		}
		
		public void widgetSelected(SelectionEvent arg0) {
			
			int selectedItem = comboLane.getSelectionIndex();
			String itemName = comboLane.getItem(selectedItem);
			Lane lane = (Lane)comboLane.getData(itemName);
			
			if( tResources.getText().length() > 0 && this.previousSelection!=null ) {
				previousSelection.setResources(Integer.parseInt(tResources.getText()));
			}
			
			previousSelection = lane;
			
			tResources.setText(""+lane.getResources());
			
			
			
			comboActivity.removeAll();
			
			String str[] = new String[lane.getActivities().size()];
			for( int i = 0; i < str.length; i++) {
				str[i] = lane.getActivities().get(i).getName();
				comboActivity.setData(str[i], lane.getActivities().get(i).getMetrics());
			}
			
			
			comboActivity.setItems(str);
			
			comboActivity.select(0);
			comboActivityListener.widgetSelected(null);
			
		}
		
	}
	
	private class ComboActivityListener implements SelectionListener
	{
		private ArrayList<Metric> previousSelection;
		
		public ComboActivityListener()
		{
			this.previousSelection = null;
		}
		
		public void widgetDefaultSelected(SelectionEvent arg0) {
			
			
		}
		
		public void widgetSelected(SelectionEvent arg0) {
			
			int selectedItem = comboActivity.getSelectionIndex();
			String itemName = comboActivity.getItem(selectedItem);
			ArrayList<Metric> activityMetrics = (ArrayList<Metric>)comboActivity.getData(itemName);
			
			if( tDuration.getText().length() > 0 && this.previousSelection!=null ) {
				previousSelection.get(0).setValue(Double.parseDouble(tDuration.getText()));
			}
			
			previousSelection = activityMetrics;
			
			tDuration.setText(""+ activityMetrics.get(0).getValue());
			
			
		}
		
	}
	
	private class StartSimulationListener implements Listener {

		public void handleEvent(Event arg0) {
			if ( arg0.type == SWT.Selection)
			{
				try {
					XMLMannager.store(NewProcess.class, new NewProcess(lanes,"SimpleProcess"), new File("C:\\SimulationData.xml"));
				} catch (JAXBException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			
		}
		
	}

}
